package com.example.drss;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.drss.adapter.RegisterAsyncTask;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class RegisterActivity extends Activity {

	EditText userText;
	EditText passwdText;
	EditText passwdText2;
	Button registerButton;

	OnClickListener listener=new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			//登录或注册
			switch (v.getId()) {
			
			case R.id.button_register_register:
			{
				//获取用户名 密码
				String user=userText.getText().toString();
				String passwd=passwdText.getText().toString();
				//用户名 密码 都不能为空
				if(!user.equals("")&&!passwd.equals(""))
				{
					if (passwdText.getText().toString().endsWith(passwdText2.getText().toString())) {
						onLogin(user, passwd);
					}
					else {
						Toast.makeText(RegisterActivity.this, "两次密码不同！", Toast.LENGTH_SHORT).show();
					}
				}
				else {
					Toast.makeText(RegisterActivity.this, "用户名或者密码不能为空", Toast.LENGTH_SHORT).show();
				}
				//提交请求：异步任务
				//启动注册窗口 Intent
			}
				break;

			default:
				break;
			}
		}
	};
	
	void onLogin(String user ,String password){
		//执行异步任务
		//http://172.24.10.130:8080/DRSSService/Login?user=test&password=123
		String url="http://172.24.10.130:8080/DRSSService/Register";
		List<NameValuePair> params= new ArrayList<NameValuePair>();
		NameValuePair userParam=new BasicNameValuePair("user",user);
		NameValuePair passwdParam=new BasicNameValuePair("password",password);
		params.add(userParam);
		params.add(passwdParam);
		RegisterAsyncTask task=new RegisterAsyncTask(RegisterActivity.this, params);
		task.execute(url);
		}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate (savedInstanceState);
		setContentView(R.layout.activity_register);
		init();
	}
	private void init(){
		userText=(EditText)findViewById(R.id.editText_register_user);
		passwdText=(EditText)findViewById(R.id.editText_register_password);
		passwdText2=(EditText)findViewById(R.id.editText_register_password2);
		registerButton=(Button)findViewById(R.id.button_register_register);
		registerButton.setOnClickListener(listener);
	}

	//🉑异步任务返回得结果
	public void onResponseLogin(String result){
		// result:Json数据
		if(result!=null){
			try {//解析Json数据
				JSONObject jsonObject=new JSONObject(result);
				//code 为0 ：登录成功
				int code=jsonObject.getInt("code");
				if(code==0){
					//进入主界面
					Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
					//Intent intent=new Intent(RegisterActivity.this,MainActivity.class);
  					//startActivity(intent);
  					finish();
				}
				else if(code==-100){
					Toast.makeText(RegisterActivity.this, "用户名已存在", Toast.LENGTH_SHORT).show();
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
}
